<?php
/**
 * 《上网记录》实现类
 *
 * @author 孙晓晔
 * @version $Id: Work.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/../log/Network.class.php');

class NetworkMy extends Network {
	
	public $orderby = array(
        '-' => '请选择',        
		'bytes' => '流量',
		'1' => '日期',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'log/network/my/find');
		return true;
	}

	function _where($request) {
		$dd_s = $request['dd_s'];
		$fx_s = $request['fx_s'];
		
		$where = " WHERE 1=1";
		if (!empty($dd_s)) {
			$where .= " AND dd >= '" . $dd_s . "'";
		}

		if (!empty($fx_s) && $fx_s <> '-') {
			$where .= " AND fx = '" . $fx_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);
		
		$dd_s = $request['dd_s'];

		$this->tbl = Constant::tbl_network_flow_daily;
		
		$emp = $_SESSION['emp'];		
		$ip = $this->_ip($emp['id']);
		// $ip = '192.168.3.30';		

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s %s %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby("dd");
		
		if (empty($where)) {
			$my = sprintf("WHERE ip = '%s'", $ip);
		} else {
			$my = sprintf(" AND ip = '%s'", $ip);
		}
		$sql = sprintf($fmt, $this->tbl, $where, $my, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];

				$row['fx'] = $this->fx[$row['fx']];
				$row['bytes'] = printsize($row['bytes']);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);
				
				$data[$count] = $row;
				$count++;
			}

			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'log/network/my/list_dd');
		return true;
	}
	
	function _ip($emp) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, Constant::tbl_emp_mac, $emp);
		$result = $db->query($sql);
		$row = $db->fetch_array($result);
		
		return $row['ip'];
	}
}
?>